package com.tech.koufu.tools;

public class CoFoolDes {
  static byte[] SA1 = 
   {1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1,
    0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1};
  static byte[] SA2 =
   {1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1};
  static byte[] SA3 =
   {1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0,
    1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1};
  static byte[] SA4 =
   {0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1,
    1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1};
  static byte[] SA5 =
   {0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0,
    0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0,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, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1};
  static byte[] SD7 =
   {0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0,
    1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0};
  static byte[] SD8 =
   {1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,
    1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1};

  static byte[][] Sc =
   {{15, 18, 12, 25, 02, 06, 04, 01, 16, 07, 22, 11, 24, 20, 13, 05, 27, 9, 17, 8, 28, 21, 14, 03,
     42, 53, 32, 38, 48, 56, 31, 41, 52, 46, 34, 49, 45, 50, 40, 29, 35, 54, 47, 43, 51, 37, 30, 33},
    {16, 19, 13, 26, 03, 07, 05, 02, 17, 8, 23, 12, 25, 21, 14, 06, 28, 10, 18, 9, 01, 22, 15, 04,
     43, 54, 33, 39, 49, 29, 32, 42, 53, 47, 35, 50, 46, 51, 41, 30, 36, 55, 48, 44, 52, 38, 31, 34},
    {18, 21, 15, 28, 05, 9, 07, 04, 19, 10, 25, 14, 27, 23, 16, 8, 02, 12, 20, 11, 03, 24, 17, 06,
     45, 56, 35, 41, 51, 31, 34, 44, 55, 49, 37, 52, 48, 53, 43, 32, 38, 29, 50, 46, 54, 40, 33, 36},
    {20, 23, 17, 02, 07, 11, 9, 06, 21, 12, 27, 16, 01, 25, 18, 10, 04, 14, 22, 13, 05, 26, 19, 8,
     47, 30, 37, 43, 53, 33, 36, 46, 29, 51, 39, 54, 50, 55, 45, 34, 40, 31, 52, 48, 56, 42, 35, 38},
    {22, 25, 19, 04, 9, 13, 11, 8, 23, 14, 01, 18, 03, 27, 20, 12, 06, 16, 24, 15, 07, 28, 21, 10,
     49, 32, 39, 45, 55, 35, 38, 48, 31, 53, 41, 56, 52, 29, 47, 36, 42, 33, 54, 50, 30, 44, 37, 40},
    {24, 27, 21, 06, 11, 15, 13, 10, 25, 16, 03, 20, 05, 01, 22, 14, 8, 18, 26, 17, 9, 02, 23, 12,
     51, 34, 41, 47, 29, 37, 40, 50, 33, 55, 43, 30, 54, 31, 49, 38, 44, 35, 56, 52, 32, 46, 39, 42},
    {26, 01, 23, 8, 13, 17, 15, 12, 27, 18, 05, 22, 07, 03, 24, 16, 10, 20, 28, 19, 11, 04, 25, 14,
     53, 36, 43, 49, 31, 39, 42, 52, 35, 29, 45, 32, 56, 33, 51, 40, 46, 37, 30, 54, 34, 48, 41, 44},
    {28, 03, 25, 10, 15, 19, 17, 14, 01, 20, 07, 24, 9, 05, 26, 18, 12, 22, 02, 21, 13, 06, 27, 16,
     55, 38, 45, 51, 33, 41, 44, 54, 37, 31, 47, 34, 30, 35, 53, 42, 48, 39, 32, 56, 36, 50, 43, 46},
    {01, 04, 26, 11, 16, 20, 18, 15, 02, 21, 8, 25, 10, 06, 27, 19, 13, 23, 03, 22, 14, 07, 28, 17,
     56, 39, 46, 52, 34, 42, 45, 55, 38, 32, 48, 35, 31, 36, 54, 43, 49, 40, 33, 29, 37, 51, 44, 47},
    {03, 06, 28, 13, 18, 22, 20, 17, 04, 23, 10, 27, 12, 8, 01, 21, 15, 25, 05, 24, 16, 9, 02, 19,
     30, 41, 48, 54, 36, 44, 47, 29, 40, 34, 50, 37, 33, 38, 56, 45, 51, 42, 35, 31, 39, 53, 46, 49},
    {05, 8, 02, 15, 20, 24, 22, 19, 06, 25, 12, 01, 14, 10, 03, 23, 17, 27, 07, 26, 18, 11, 04, 21,
     32, 43, 50, 56, 38, 46, 49, 31, 42, 36, 52, 39, 35, 40, 30, 47, 53, 44, 37, 33, 41, 55, 48, 51},
    {07, 10, 04, 17, 22, 26, 24, 21, 8, 27, 14, 03, 16, 12, 05, 25, 19, 01, 9, 28, 20, 13, 06, 23,
     34, 45, 52, 30, 40, 48, 51, 33, 44, 38, 54, 41, 37, 42, 32, 49, 55, 46, 39, 35, 43, 29, 50, 53},
    {9, 12, 06, 19, 24, 28, 26, 23, 10, 01, 16, 05, 18, 14, 07, 27, 21, 03, 11, 02, 22, 15, 8, 25,
     36, 47, 54, 32, 42, 50, 53, 35, 46, 40, 56, 43, 39, 44, 34, 51, 29, 48, 41, 37, 45, 31, 52, 55},
    {11, 14, 8, 21, 26, 02, 28, 25, 12, 03, 18, 07, 20, 16, 9, 01, 23, 05, 13, 04, 24, 17, 10, 27,
     38, 49, 56, 34, 44, 52, 55, 37, 48, 42, 30, 45, 41, 46, 36, 53, 31, 50, 43, 39, 47, 33, 54, 29},
    {13, 16, 10, 23, 28, 04, 02, 27, 14, 05, 20, 9, 22, 18, 11, 03, 25, 07, 15, 06, 26, 19, 12, 01,
     40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 56, 31},
    {14, 17, 11, 24, 01, 05, 03, 28, 15, 06, 21, 10, 23, 19, 12, 04, 26, 8, 16, 07, 27, 20, 13, 02,
     41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32}};

	static byte[][] G = new byte[17][49];
	static byte[] L = new byte[33];
	static byte[] R = new byte[33];
	static byte[] F = new byte[33];
	static byte[] C = new byte[57];
	
	public static byte Ord(boolean input)
	{
		if(input) return 1;
		return 0;
	}
	public static void DES_Init(byte[] Key, boolean FCode)
	{
		byte n, h;
		C[1]  = Ord((Key[7] & 128) > 0); C[29] = Ord((Key[7] & 2) > 0);
		C[2]  = Ord((Key[6] & 128) > 0); C[30] = Ord((Key[6] & 2) > 0);
		C[3]  = Ord((Key[5] & 128) > 0); C[31] = Ord((Key[5] & 2) > 0);
		C[4]  = Ord((Key[4] & 128) > 0); C[32] = Ord((Key[4] & 2) > 0);
		C[5]  = Ord((Key[3] & 128) > 0); C[33] = Ord((Key[3] & 2) > 0);
		C[6]  = Ord((Key[2] & 128) > 0); C[34] = Ord((Key[2] & 2) > 0);
		C[7]  = Ord((Key[1] & 128) > 0); C[35] = Ord((Key[1] & 2) > 0);
		C[8]  = Ord((Key[0] & 128) > 0); C[36] = Ord((Key[0] & 2) > 0);
		
		C[9]  = Ord((Key[7] & 64) > 0);  C[37] = Ord((Key[7] & 4) > 0);
		C[10] = Ord((Key[6] & 64) > 0);  C[38] = Ord((Key[6] & 4) > 0);
		C[11] = Ord((Key[5] & 64) > 0);  C[39] = Ord((Key[5] & 4) > 0);
		C[12] = Ord((Key[4] & 64) > 0);  C[40] = Ord((Key[4] & 4) > 0);
		C[13] = Ord((Key[3] & 64) > 0);  C[41] = Ord((Key[3] & 4) > 0);
		C[14] = Ord((Key[2] & 64) > 0);  C[42] = Ord((Key[2] & 4) > 0);
		C[15] = Ord((Key[1] & 64) > 0);  C[43] = Ord((Key[1] & 4) > 0);
		C[16] = Ord((Key[0] & 64) > 0);  C[44] = Ord((Key[0] & 4) > 0);
		
		C[17] = Ord((Key[7] & 32) > 0);  C[45] = Ord((Key[7] & 8) > 0);
		C[18] = Ord((Key[6] & 32) > 0);  C[46] = Ord((Key[6] & 8) > 0);
		C[19] = Ord((Key[5] & 32) > 0);  C[47] = Ord((Key[5] & 8) > 0);
		C[20] = Ord((Key[4] & 32) > 0);  C[48] = Ord((Key[4] & 8) > 0);
		C[21] = Ord((Key[3] & 32) > 0);  C[49] = Ord((Key[3] & 8) > 0);
		C[22] = Ord((Key[2] & 32) > 0);  C[50] = Ord((Key[2] & 8) > 0);
		C[23] = Ord((Key[1] & 32) > 0);  C[51] = Ord((Key[1] & 8) > 0);
		C[24] = Ord((Key[0] & 32) > 0);  C[52] = Ord((Key[0] & 8) > 0);
		
		C[25] = Ord((Key[7] & 16) > 0);  C[53] = Ord((Key[3] & 16) > 0);
		C[26] = Ord((Key[6] & 16) > 0);  C[54] = Ord((Key[2] & 16) > 0);
		C[27] = Ord((Key[5] & 16) > 0);  C[55] = Ord((Key[1] & 16) > 0);
		C[28] = Ord((Key[4] & 16) > 0);  C[56] = Ord((Key[0] & 16) > 0);
		
		
		if(FCode)
		{
			for(n=1; n<=16; n++)
			{
				for(h=1; h<=48; h++)
				{
					G[n][h] = C[Sc[n-1][h-1]];
				}
			}
		}
		else
		{
			for(n=1; n<=16; n++)
			{
				for(h=1; h<=48; h++)
				{
					G[17 - n][h] = C[Sc[n-1][h-1]];
				}
			}
		}
	}
     
	public static void DES_Code(byte[] Input, byte[]  Output)
	{
		byte n;
		int  z;
		L[1] = Ord((Input[7] & 64) > 0); R[1] = Ord((Input[7] & 128) > 0);
		L[2] = Ord((Input[6] & 64) > 0); R[2] = Ord((Input[6] & 128) > 0);
		L[3] = Ord((Input[5] & 64) > 0); R[3] = Ord((Input[5] & 128) > 0);
		L[4] = Ord((Input[4] & 64) > 0); R[4] = Ord((Input[4] & 128) > 0);
		L[5] = Ord((Input[3] & 64) > 0); R[5] = Ord((Input[3] & 128) > 0);
		L[6] = Ord((Input[2] & 64) > 0); R[6] = Ord((Input[2] & 128) > 0);
		L[7] = Ord((Input[1] & 64) > 0); R[7] = Ord((Input[1] & 128) > 0);
		L[8] = Ord((Input[0] & 64) > 0); R[8] = Ord((Input[0] & 128) > 0);
		L[9] = Ord((Input[7] & 16) > 0); R[9] = Ord((Input[7] & 32) > 0);
		L[10]= Ord((Input[6] & 16) > 0); R[10]= Ord((Input[6] & 32) > 0);
		L[11]= Ord((Input[5] & 16) > 0); R[11]= Ord((Input[5] & 32) > 0);
		L[12]= Ord((Input[4] & 16) > 0); R[12]= Ord((Input[4] & 32) > 0);
		L[13]= Ord((Input[3] & 16) > 0); R[13]= Ord((Input[3] & 32) > 0);
		L[14]= Ord((Input[2] & 16) > 0); R[14]= Ord((Input[2] & 32) > 0);
		L[15]= Ord((Input[1] & 16) > 0); R[15]= Ord((Input[1] & 32) > 0);
		L[16]= Ord((Input[0] & 16) > 0); R[16]= Ord((Input[0] & 32) > 0);
		L[17]= Ord((Input[7] & 4) > 0);  R[17]= Ord((Input[7] & 8) > 0);
		L[18]= Ord((Input[6] & 4) > 0);  R[18]= Ord((Input[6] & 8) > 0);
		L[19]= Ord((Input[5] & 4) > 0);  R[19]= Ord((Input[5] & 8) > 0);
		L[20]= Ord((Input[4] & 4) > 0);  R[20]= Ord((Input[4] & 8) > 0);
		L[21]= Ord((Input[3] & 4) > 0);  R[21]= Ord((Input[3] & 8) > 0);
		L[22]= Ord((Input[2] & 4) > 0);  R[22]= Ord((Input[2] & 8) > 0);
		L[23]= Ord((Input[1] & 4) > 0);  R[23]= Ord((Input[1] & 8) > 0);
		L[24]= Ord((Input[0] & 4) > 0);  R[24]= Ord((Input[0] & 8) > 0);
		L[25]= (byte)(Input[7] & (byte)1);           R[25]= Ord((Input[7] & 2) > 0);
		L[26]= (byte)(Input[6] & (byte)1);           R[26]= Ord((Input[6] & 2) > 0);
		L[27]= (byte)(Input[5] & (byte)1);           R[27]= Ord((Input[5] & 2) > 0);
		L[28]= (byte)(Input[4] & (byte)1);           R[28]= Ord((Input[4] & 2) > 0);
		L[29]= (byte)(Input[3] & (byte)1);           R[29]= Ord((Input[3] & 2) > 0);
		L[30]= (byte)(Input[2] & (byte)1);           R[30]= Ord((Input[2] & 2) > 0);
		L[31]= (byte)(Input[1] & (byte)1);           R[31]= Ord((Input[1] & 2) > 0);
		L[32]= (byte)(Input[0] & (byte)1);           R[32]= Ord((Input[0] & 2) > 0);
		
		for(n=1;n<=16;n++)
		{
			z = ((R[32]  ^ G[n][1])  << 5) | ((R[5]  ^ G[n][6])  << 4)
			  | ((R[ 1]  ^ G[n][2])  << 3) | ((R[2]  ^ G[n][3])  << 2)
			  | ((R[ 3]  ^ G[n][4])  << 1) | ( R[4]  ^ G[n][5]);
			F[ 9] = (byte)(L[ 9]  ^ SA1[z]);
			F[17] = (byte)(L[17]  ^ SB1[z]);
			F[23] = (byte)(L[23]  ^ SC1[z]);
			F[31] = (byte)(L[31]  ^ SD1[z]);
			
			z = ((R[4]  ^ G[n][ 7])  << 5) | ((R[9]  ^ G[n][12])  << 4)
			  | ((R[5]  ^ G[n][ 8])  << 3) | ((R[6]  ^ G[n][ 9])  << 2)
			  | ((R[7]  ^ G[n][10])  << 1) | ( R[8]  ^ G[n][11]);
			F[13] = (byte)(L[13]  ^ SA2[z]);
			F[28] = (byte)(L[28]  ^ SB2[z]);
			F[ 2] = (byte)(L[ 2]  ^ SC2[z]);
			F[18] = (byte)(L[18]  ^ SD2[z]);
			
			z = ((R[ 8]  ^ G[n][13])  << 5) | ((R[13]  ^ G[n][18])  << 4)
			  | ((R[ 9]  ^ G[n][14])  << 3) | ((R[10]  ^ G[n][15])  << 2)
			  | ((R[11]  ^ G[n][16])  << 1) | ( R[12]  ^ G[n][17]);
			F[24] = (byte)(L[24]  ^ SA3[z]);
			F[16] = (byte)(L[16]  ^ SB3[z]);
			F[30] = (byte)(L[30]  ^ SC3[z]);
			F[ 6] = (byte)(L[ 6]  ^ SD3[z]);
			
			z = ((R[12]  ^ G[n][19])  << 5) | ((R[17]  ^ G[n][24])  << 4)
			  | ((R[13]  ^ G[n][20])  << 3) | ((R[14]  ^ G[n][21])  << 2)
			  | ((R[15]  ^ G[n][22])  << 1) | ( R[16]  ^ G[n][23]);
			F[26] = (byte)(L[26]  ^ SA4[z]);
			F[20] = (byte)(L[20]  ^ SB4[z]);
			F[10] = (byte)(L[10]  ^ SC4[z]);
			F[ 1] = (byte)(L[ 1]  ^ SD4[z]);
			
			z = ((R[16]  ^ G[n][25])  << 5) | ((R[21]  ^ G[n][30])  << 4)
			  | ((R[17]  ^ G[n][26])  << 3) | ((R[18]  ^ G[n][27])  << 2)
			  | ((R[19]  ^ G[n][28])  << 1) | ( R[20]  ^ G[n][29]);
			F[ 8] = (byte)(L[ 8]  ^ SA5[z]);
			F[14] = (byte)(L[14]  ^ SB5[z]);
			F[25] = (byte)(L[25]  ^ SC5[z]);
			F[ 3] = (byte)(L[ 3]  ^ SD5[z]);
			
			z = ((R[20]  ^ G[n][31])  << 5) | ((R[25]  ^ G[n][36])  << 4)
			  | ((R[21]  ^ G[n][32])  << 3) | ((R[22]  ^ G[n][33])  << 2)
			  | ((R[23]  ^ G[n][34])  << 1) | ( R[24]  ^ G[n][35]);
			F[ 4] = (byte)(L[ 4]  ^ SA6[z]);
			F[29] = (byte)(L[29]  ^ SB6[z]);
			F[11] = (byte)(L[11]  ^ SC6[z]);
			F[19] = (byte)(L[19]  ^ SD6[z]);
			
			z = ((R[24]  ^ G[n][37])  << 5) | ((R[29]  ^ G[n][42])  << 4)
			  | ((R[25]  ^ G[n][38])  << 3) | ((R[26]  ^ G[n][39])  << 2)
			  | ((R[27]  ^ G[n][40])  << 1) | ( R[28]  ^ G[n][41]);
			F[32] = (byte)(L[32]  ^ SA7[z]);
			F[12] = (byte)(L[12]  ^ SB7[z]);
			F[22] = (byte)(L[22]  ^ SC7[z]);
			F[ 7] = (byte)(L[ 7]  ^ SD7[z]);
			
			z = ((R[28]  ^ G[n][43])  << 5) | ((R[ 1]  ^ G[n][48])  << 4)
			  | ((R[29]  ^ G[n][44])  << 3) | ((R[30]  ^ G[n][45])  << 2)
			  | ((R[31]  ^ G[n][46])  << 1) | ( R[32]  ^ G[n][47]);
			F[ 5] = (byte)(L[ 5]  ^ SA8[z]);
			F[27] = (byte)(L[27]  ^ SB8[z]);
			F[15] = (byte)(L[15]  ^ SC8[z]);
			F[21] = (byte)(L[21]  ^ SD8[z]);
			
			for(int x=0; x<L.length; x++){	L[x] = R[x];R[x] = F[x];	}
		}

		Output[0] = (byte)((L[8] << 7) | (R[8] << 6) | (L[16] << 5) | (R[16] << 4)
		| (L[24] << 3) | (R[24] << 2) | (L[32] << 1) | R[32]);
		Output[1] = (byte)((L[7] << 7) | (R[7] << 6) | (L[15] << 5) | (R[15] << 4)
		| (L[23] << 3) | (R[23] << 2) | (L[31] << 1) | R[31]);
		Output[2] = (byte)((L[6] << 7) | (R[6] << 6) | (L[14] << 5) | (R[14] << 4)
		| (L[22] << 3) | (R[22] << 2) | (L[30] << 1) | R[30]);
		Output[3] = (byte)((L[5] << 7) | (R[5] << 6) | (L[13] << 5) | (R[13] << 4)
		| (L[21] << 3) | (R[21] << 2) | (L[29] << 1) | R[29]);
		Output[4] = (byte)((L[4] << 7) | (R[4] << 6) | (L[12] << 5) | (R[12] << 4)
		| (L[20] << 3) | (R[20] << 2) | (L[28] << 1) | R[28]);
		Output[5] = (byte)((L[3] << 7) | (R[3] << 6) | (L[11] << 5) | (R[11] << 4)
		| (L[19] << 3) | (R[19] << 2) | (L[27] << 1) | R[27]);
		Output[6] = (byte)((L[2] << 7) | (R[2] << 6) | (L[10] << 5) | (R[10] << 4)
		| (L[18] << 3) | (R[18] << 2) | (L[26] << 1) | R[26]);
		Output[7] = (byte)((L[1] << 7) | (R[1] << 6) | (L[9] << 5) | (R[9] << 4)
		| (L[17] << 3) | (R[17] << 2) | (L[25] << 1) | R[25]);
		
	}

	public static byte[] StrToKey(byte[] aKey)
	{
		byte[] Key = new byte[8];
		int I;
		for(int i=0; i<8; i++) Key[i] = 0;
		for(I = 1; I<=aKey.length; I++)
		{
			Key[I % 8] = (byte)(Key[I % 8] + aKey[I-1]);
		}
		return Key;
	}
	
    private final static char[] mChars = "0123456789ABCDEF".toCharArray();  
    private final static String mHexStr = "0123456789ABCDEF";    

	public static String DES_EnCrypt(String str, String aKey)
	{
		byte[] temp_key = aKey.getBytes();
		byte[] Key = StrToKey(temp_key);
		DES_Init(Key, true);
		byte[] aStr = str.getBytes();
		int Count = aStr.length;
		int Offset = 0;
		int i;
		
		byte[] S = new byte[8];
		byte[] WriteBuf = new byte[8];
		byte[] ReadBuf = new byte[8];
		
		String result = "";
		while(true)
		{
			for(i=0; i<8; i++) S[i] = 0;
			for(i=Offset; i<Offset+8 && i<Count ; i++) S[i - Offset] = aStr[i];
			for(i=0; i<8; i++) ReadBuf[i] = S[i];
			DES_Code(ReadBuf, WriteBuf);

			for(i=0; i<=7; i++)
				result = result + mChars[(WriteBuf[i] & 0xFF) >> 4] + mChars[WriteBuf[i] & 0x0F];
			Offset = Offset + 8;
			if( Offset >= Count) break;
		}
		return result;
	}
	public static void main(String[] args) throws Exception {
		String key = "5fd9p";
		String input = "366X5652201340";
		CoFoolDes crypt = new CoFoolDes();
		System.out.println("Encode:" + crypt.DES_EnCrypt(input, key));
	}

}